import {createRouter, createWebHistory} from 'vue-router'
import {ElMessage, ElNotification} from "element-plus";
import request from "../utils/Request.js";
import store from "../store/index.js";
import * as path from "path";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            name: 'Index',
            component: () => import('../views/home/Index.vue'),
            children: [
                {
                    path: '/',
                    name: 'Home',
                    component: () => import('../views/home/Home.vue'),
                    meta: {
                        title: '柿子文档'
                    }
                },
                {
                    path: '/discover',
                    name: 'Discover',
                    component: () => import('../views/home/Discover.vue'),
                    meta: {
                        title: '发现 | 柿子文档'
                    }
                },
                {
                    path: '/about',
                    name: 'About',
                    component: () => import('../views/home/About.vue'),
                    meta: {
                        title: '关于 | 柿子文档'
                    }
                },
            ]
        },
        {
            path: '/:catchAll(.*)', // This will match any path that hasn't been matched above
            name: '404',
            component: () => import('../views/error/404.vue'),
            meta: {
                title: '404 Not Found'
            }
        }, {
            path: '/403', // This will match any path that hasn't been matched above
            name: '403',
            component: () => import('../views/error/403.vue'),
            meta: {
                title: '403 Forbidden'
            }
        },
        {
            path: '/login',
            name: 'Login',
            component: () => import('../views/login/Login.vue'),
            meta: {
                title: '登录 | 柿子文档'
            }
        },
        {
            path: '/logout',
            name: 'Logout',
            component: () => import('../views/login/Logout.vue'),
            meta: {
                title: '正在退出登录...'
            }
        },
        {
            path: '/reg',
            name: 'Reg',
            component: () => import('../views/login/Reg.vue'),
            meta: {
                title: '注册 | 柿子文档'
            }
        },
        {
            path: '/forget',
            name: 'Forget',
            component: () => import('../views/login/Forget.vue'),
            meta: {
                title: '忘记密码 | 柿子文档'
            }
        },
        {
            path: '/term/service',
            name: 'ServiceTerm',
            component: () => import('../views/term/Service.vue'),
            meta: {
                title: '服务条款 | 柿子文档'
            }
        },
        {
            path: '/term/privacy',
            name: 'PrivacyTerm',
            component: () => import('../views/term/Privacy.vue'),
            meta: {
                title: '隐私协议 | 柿子文档'
            }
        },
        {
            path: '/oauth2/callback/:regId',
            name: 'OAuth2Callback',
            component: () => import('../views/login/OAuth2Callback.vue'),
            meta: {
                title: '正在第三方登录...'
            }
        },
        {
            path: '/project',
            name: 'ProjectIndex',
            component: () => import('../views/project/Index.vue'),
            children: [
                {
                    path: '',
                    name: 'ProjectMenu',
                    component: () => import('../views/project/Menu.vue'),
                    children: [
                        {
                            path: '/project',
                            name: 'Project',
                            component: () => import('../views/project/Project.vue'),
                            meta: {
                                title: '知识库管理 | 柿子文档'
                            }
                        },
                        {
                            path: '/apiTest',
                            name: 'ApiTest',
                            component: () => import('../views/project/ApiTest.vue'),
                            meta: {
                                title: '接口测试 | 柿子文档'
                            }
                        },
                        {
                            path: '/mock',
                            name: 'Mock',
                            component: () => import('../views/project/Mock.vue'),
                            meta: {
                                title: 'Mock | 柿子文档'
                            }
                        },
                        {
                            path: '/sampleTest',
                            name: 'SampleTest',
                            component: () => import('../views/project/SampleTest.vue'),
                            meta: {
                                title: '用例测试 | 柿子文档'
                            }
                        }
                    ]
                },
                {
                    path: '/message',
                    name: 'Message',
                    component: () => import('../views/project/Message.vue'),
                    meta: {
                        title: '消息通知 | 柿子文档'
                    }
                },
                {
                    path: '/user',
                    name: 'User',
                    component: () => import('../views/project/User.vue'),
                    meta: {
                        title: '个人信息 | 柿子文档'
                    }
                },
                {
                    path: '/p/:projectId',
                    name: 'Edit',
                    component: () => import('../views/project/edit/Edit.vue'),
                    meta: {
                        title: '知识库编辑 | 柿子文档'
                    }
                }
            ]
        },
        {
            path: '/v/:projectId',
            name: 'View',
            component: () => import('../views/view/View.vue'),
            meta: {
                title: '文档预览 | 柿子文档'
            }
        },
    ]
})

const permitPages = ['ServiceTerm', 'PrivacyTerm', 'RefusePage', 'Index', 'Home', 'Discover', 'About', '404', '403', 'OAuth2Callback', 'View']
const anonymousPages = ['Login', 'Reg', 'Forget']

router.beforeEach((to, from, next) => {
    let token = localStorage.getItem('token')
    if (token == null) {
        if (!(permitPages.includes(to.name) || anonymousPages.includes(to.name))) {
            next({name: 'Login'})
            return
        }
    } else {
        if (anonymousPages.includes(to.name)) {
            next({name: 'Home'})
            return
        }
    }
    next()
})

const noGetPages = ['ServiceTerm', 'PrivacyTerm', '404', '403']

router.afterEach((to, from, failure) => {
    // 为每一个页面设置title
    if (to.meta.title)
        document.title = to.meta.title

    // 这些页面不需要检查获取user, 防止循环错误
    if (noGetPages.includes(to.name))
        return;

    let userData;
    // 解析localStorage中的userData
    try {
        userData = JSON.parse(localStorage.getItem("userData"));
    } catch (e) {
        userData = null
    }


    let token = localStorage.getItem("token")
    if (token != null) {
        // 找不到userData  请求
        if (userData == null || userData.id == null || userData.nickname == null) {

        } else if (store.state.user == null || Object.keys(store.state.user).length === 0) {
            store.commit('saveUserData', userData)
        }

    }
})

export default router
